const fs = require('fs')
var tool,h,_dir,_POST
exports.main = (req,res) =>{
    _dir = req.bind.dir
    _dir = _dir.replace(/\\/g,'/')
    _POST = req.bind.__POST
    tool = require(_dir+'/module/emma.js')
    //  ajax处理
    if(_POST && _POST['ajaxid']){
        this.ajax(_POST,res);
        return;
    }
    //  form 表单后台处理
    else if(_POST && _POST['fromid']){
        this.form(_POST,res);
        return;
    }
    const HTML = tool.require('html')
    h = new HTML()
    h.isMobile(req,tool)
    h.load({'title':'系统管理|'+Math.random()*10000,'js':['/public/jquery-1.12.4.min','/public/jutil','/public/js/admin'],'css':'/public/css/admin'})
    h.appHead('<meta charset="utf-8">')
    var html = ''
    html += `<nav>
        <a href="javascript:void(0);" id="newPageBtn">新建模块</a>
    </nav>`
    html += pagelist()
    html += newPageFrom()
    h.appBody(html)
    res.write(h.getHtml())
}
function newPageFrom(){
    var html = '<form method="post" id="newPageForm">'
    html += '<br><lable>js页面：</lable><span id="pname_prefix">'+_dir+'/app/</span>'+h.text([
        {'type':'text',required:'required',name:'page',id:'page_name'},
        {'type':'hidden',name:'fromid',value:'from2newPage'},
        {'type':'hidden',name:'prefix',value:_dir+'/app/'},
    ])
    html += `<p>
        前端
        <br><a href="javascript:void(0);" class="addFront">JS</a>
        <br><a href="javascript:void(0);" class="addFront">CSS</a>
        <br><a href="javascript:void(0);" class="addFront">HTML</a>
    </p>`
    html += '<br>'+h.text({'type':'submit',value:'保存'})
    html += '</from>'
    return html
}
function pagelist(){
    var rdir = _dir+'/app/'
    var html = '',
        data = fs.readdirSync(rdir),
        name
    const stats = fs.stats
    for(var k in data){
        name = rdir+data[k]
        html += `<li>${data[k]}</li>`
    }
    return html
}
//  ajax 请求
exports.ajax = (data,res) =>{
    var ret = ''
    if(!data['ajaxid']) ret = '非法ajax数据请求'
    var rid = data['ajaxid']
    if('pageNameBlur' == rid){
        var fname = data['value']
        if(fname.indexOf('.js') == -1) fname += '.js'
        if(fs.existsSync(fname)) ret = 'Y'
        else ret = fname
    }
    res.write(ret)   
}
//  form 数据处理
exports.form = (data,res) =>{
    var fromid = data['fromid'],
        ret = ''
    if('from2newPage' == fromid){
        var prefix = data['prefix']
        var page = prefix+data['page']
        page += page.indexOf('.js')>-1? '':'.js'
        var files = [page]
        if(data['js']) files.push(data['js'])
        if(data['css']) files.push(data['css'])
        if(data['html']) files.push(data['html'])
        for(var k in files){
            if(!fs.existsSync(files[k])){
                if(fs.writeFileSync(files[k],'')) ret += `成功创建：${files[k]}<br>`
                else ret += `${files[k]}无法创建<br>`
            }
            else ret += `${files[k]}文件已经存在！<br>`            
        }
        if(ret) ret = '<meta charset="utf-8">'+ret
        res.write(ret)
    }    
}